// https://www.cnblogs.com/yurui321/p/14894161.html
export default {
  getDaysDiffBetweenDates(start, end) {
    // 时间间隔天数
    return (end - start) / (1000 * 3600 * 24);
  },
  dateFormat(t, formats = "Y-m-d H:i:s") {
    // 日期格式化
    var zero = function(v) {
      return v < 10 ? "0" + v : v;
    };
    var d = t ? new Date(t) : new Date();
    return formats.replace(/Y|m|d|H|i|s/gi, function(matches) {
      return {
        Y: d.getFullYear(),
        m: zero(d.getMonth() + 1),
        d: zero(d.getDate()),
        H: zero(d.getHours()),
        i: zero(d.getMinutes()),
        s: zero(d.getSeconds())
      }[matches];
    });
  },

  formatPassTime(startTime) {
    // 距现在的已过时间
    var currentTime = Date.parse(new Date()),
      time = currentTime - startTime,
      day = parseInt(time / (1000 * 60 * 60 * 24)),
      hour = parseInt(time / (1000 * 60 * 60)),
      min = parseInt(time / (1000 * 60)),
      month = parseInt(day / 30),
      year = parseInt(month / 12);
    if (year) return year + "年前";
    if (month) return month + "个月前";
    if (day) return day + "天前";
    if (hour) return hour + "小时前";
    if (min) return min + "分钟前";
    else return "刚刚";
  },
  formatRemainTime(endTime) {
    // 距${endTime}的剩余时间
    var startDate = new Date(); //开始时间
    var endDate = new Date(endTime); //结束时间
    var t = endDate.getTime() - startDate.getTime(); //时间差
    var d = 0,
      h = 0,
      m = 0,
      s = 0;
    if (t >= 0) {
      d = Math.floor(t / 1000 / 3600 / 24);
      h = Math.floor(t / 1000 / 60 / 60 % 24);
      m = Math.floor(t / 1000 / 60 % 60);
      s = Math.floor(t / 1000 % 60);
    }
    return d + "天 " + h + "小时 " + m + "分钟 " + s + "秒";
  },

  monthDays(time) {
    // 获取指定日期月份的总天数
    time = new Date(time);
    var year = time.getFullYear();
    var month = time.getMonth() + 1;
    return new Date(year, month, 0).getDate();
  },
  uniqueId() {
    // 随机数时间戳
    var a = Math.random,
      b = parseInt;
    return (
      Number(new Date()).toString() + b(10 * a()) + b(10 * a()) + b(10 * a())
    );
  }
};
